﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Bitirme.DAL.Entities;
using Bitirme.DAL.Enum;
using Bitirme.DAL.Data_Access;
using Bitirme.DAL.Utils;

namespace Bitirme.Controllers
{
    public class ProjectHandler
    {
        public ProjectHandler()
        {

        }
        public int InsertProject(string Name, string Subject, string Details, int ProjectState, int Duration, decimal Cost, int PMID, string StartDate, string EndDate)
        {
            Project project = new Project();
            project.Name = Name;
            project.Subject = Subject;
            project.Details = Details;
            project.Duration = Duration;
            project.Cost = Cost;
            switch (ProjectState)
            {
                case 0:
                    project.ProjectState = State.None;
                    break;
                case 1:
                    project.ProjectState = State.Active;
                    break;
                case 2:
                    project.ProjectState = State.Pasive;
                    break;
                case 3:
                    project.ProjectState = State.Suspended;
                    break;
                default:
                    project.ProjectState = State.None;
                    break;
            }

            project.Manager = new ProjectManager();
            project.Manager.ID = PMID;
            DateTime? date = DateUtils.TransformTextToDateTime(StartDate);
            int year = date.Value.Year;
            int month = date.Value.Month;
            int day = date.Value.Day;
            int hours = 0;
            int minutes = 0;
            int seconds = 0;
            project.StartDate = new DateTime(year, month, day, hours, minutes, seconds);
            date = DateUtils.TransformTextToDateTime(EndDate);
            year = date.Value.Year;
            month = date.Value.Month;
            day = date.Value.Day;
            project.EndDate = new DateTime(year, month, day, hours, minutes, seconds);

            

            try
            {

                return ProjectCollection.InsertProject(project);

            }

            catch
            {

                throw;

            }
        } 
        public List<Project> GetAllProjects()
        {
            return ProjectCollection.GetAllProjects();
        }

        public Project SelectProjectByID(int id){

            return ProjectCollection.SelectProjectByID(id);
        }

        /*** Bir projeyi tüm attributeleriyle birlikte getirir ***/
        public Project GetProjectsWithAllAttributesByID(int id)
        {
            Project project;
            /*** normal attributeleri getirilir ***/
            project = ProjectCollection.SelectProjectByID(id);

            /*** workpackageları getirilir ***/
            WorkPackageHandler wpHandler = new WorkPackageHandler();
            project.PackageList = wpHandler.GetAllWorkPackagesWithRequirementsAndDependenciesByProjectID(id);

            /*** Assignement listesi getirilir ***/
            AssignmentHandler asHandler = new AssignmentHandler();
            project.AssignmentList = asHandler.GetAssignmentsByProjectID(id);

            /*** TODO: assignementlar gelirken emplooyee ve workpackagelar la beraber gelsin ***/
            /*** Workpackagelar gelirken id'lerini getir yukarıdaki workpackagelist ile karşılaştırarak nesneleri bulalım ***/
            /*** Employee için de aynı şey gerekiyor sanırım ***/
            /*** Bunu yapmamızın sebebi ağaç yapısı içinde search yapabilmek diye düşünüyorum ***/
            /*** Edit-1: Ağaç yapısı gerçekten gerekli mi??? ***/

            /*** Listedeki assignmentların workpackageları düzenleniyor ***/
            foreach (Assignment assignment in project.AssignmentList)
            {
                foreach (WorkPackage package in project.PackageList)
                {
                    if (assignment.WorkPackage.ID == package.ID)
                    {
                        assignment.WorkPackage = package;
                        break;
                    }
                }
            }

            return project;
        }

        public Boolean DeleteProjectByID(int projectID) {
            int id = projectID;
            return ProjectCollection.DeleteProjectByID(projectID);
        }

        public int UpdateProjectByID(int ID, string Name, string Subject, string Details, int ProjectState, int Duration, decimal Cost, int PMID, string StartDate, string EndDate) {

            Project project = new Project();
            project.ID = ID;
            project.Name = Name;
            project.Subject = Subject;
            project.Details = Details;
            project.Duration = Duration;
            project.Cost = Cost;
            switch (ProjectState)
            {
                case 0:
                    project.ProjectState = State.None;
                    break;
                case 1:
                    project.ProjectState = State.Active;
                    break;
                case 2:
                    project.ProjectState = State.Pasive;
                    break;
                case 3:
                    project.ProjectState = State.Suspended;
                    break;
                default:
                    project.ProjectState = State.None;
                    break;
            }

            project.Manager = new ProjectManager();
            project.Manager.ID = PMID;
            DateTime? date = DateUtils.TransformTextToDateTime(StartDate);
            int year = date.Value.Year;
            int month = date.Value.Month;
            int day = date.Value.Day;
            int hours = 0;
            int minutes = 0;
            int seconds = 0;
            project.StartDate = new DateTime(year, month, day, hours, minutes, seconds);
            date = DateUtils.TransformTextToDateTime(EndDate);
            year = date.Value.Year;
            month = date.Value.Month;
            day = date.Value.Day;
            project.EndDate = new DateTime(year, month, day, hours, minutes, seconds);



            try
            {

                return ProjectCollection.UpdateProjectByID(project);

            }

            catch
            {

                throw;

            }
        }
    }

}